package com.fresh.service.Impl;

import com.fresh.constant.MessageConstant;
import com.fresh.constant.StatusConstant;
import com.fresh.context.BaseContext;
import com.fresh.dto.UserLoginDTO;
import com.fresh.dto.UserPageQueryDTO;
import com.fresh.dto.UserRegisterDTO;
import com.fresh.dto.UserUpdateDTO;
import com.fresh.exception.AccountLockedException;
import com.fresh.exception.AccountNotFoundException;
import com.fresh.exception.PasswordErrorException;
import com.fresh.mapper.UserMapper;
import com.fresh.pojo.User;
import com.fresh.result.PageResult;
import com.fresh.service.UserService;
import com.fresh.vo.UserLoginVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * @author Long
 * @title UserServiceImpl
 * @date 2024/07/01 19:34
 * @description TODO
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 登录功能
     * @param userLoginDTO
     * @return
     */
    @Override
    public User login(UserLoginDTO userLoginDTO) {

        String userName = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        User user = userMapper.getUserByUserName(userName);

        if (user == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        //对前端传过来的明文密码进行md5加密处理
        String password1 = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        if (user.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return user;

    }

    /**
     * 注册功能
     * @param userRegisterDTO
     */
    @Override
    public void register(UserRegisterDTO userRegisterDTO) {

        // 创建用户对象
        User user = new User();
        BeanUtils.copyProperties(userRegisterDTO, user);
        user.setStatus(StatusConstant.ENABLE);
        userMapper.insertUser(user);
    }

    /**
     * 员工分页查询
     */
    @Override
    public PageResult query(UserPageQueryDTO userPageQueryDTO) {
        // 开始分页查询
        PageHelper.startPage(userPageQueryDTO.getPage(), userPageQueryDTO.getPageSize());

        //调用方法
        Page<User> page = userMapper.query(userPageQueryDTO);

        // 加工处理
        long total = page.getTotal();
        List<User> records = page.getResult();
        PageResult pageResult = new PageResult(total, records);

        return pageResult;
    }

    /**
     * 修改用户数据
     * @param userUpdateDTO
     */
    @Override
    public void update(UserUpdateDTO userUpdateDTO) {
        User user = User.builder().build();
        BeanUtils.copyProperties(userUpdateDTO, user);
        userMapper.update(user);
    }

    /**
     * 修改用户状态
     * @param status
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        User user = User.builder()
                .id(id)
                .status(status)
                .build();
        userMapper.update(user);
    }
}
